﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using X13.Libs;

namespace X13.Mapping
{
    public sealed class Minefield : GameComponent, ISelectable3DCollection
    {
        //Fields
        private MineTile[,] _DataMatrix;
        public MineTile this[int x, int y] { get { return _DataMatrix[y, x]; } }

        public int Width { get { return _DataMatrix.GetLength(1); } }
        public int Depth { get { return _DataMatrix.GetLength(0); } }

        //Construct
        public Minefield(Game game, int[,] dataMatrix)
            : base(game)
        {
            CreateFrom(dataMatrix);
        }

        //Methods
        private void CreateFrom(int[,] data)
        {
            _DataMatrix = new MineTile[data.GetLength(1), data.GetLength(0)];

            for (int y = 0; y < data.GetLength(0); y++)
            {
                for (int x = 0; x < data.GetLength(1); x++)
                {
                    int index = data[y, x];

                    MineTile t = new MineTile(new MinefieldPosition(x, y));
                    t.MineState = (MineState)index;

                    _DataMatrix[y, x] = t;
                }
            }
        }

        public void DrawMinefield()
        {
            for (int y = 0; y < Depth; y++)
                for (int x = 0; x < Width; x++)
                    this[x, y].RenderCell();
        }
        public void UpdateMinefield()
        {
            for (int y = 0; y < Depth; y++)
                for (int x = 0; x < Width; x++)
                    this[x, y].RenderCell();
        }
        
        public IEnumerable<ISelectable3D> GetSelectables()
        {
            for (int y = 0; y < Depth; y++)
                for (int x = 0; x < Width; x++)
                    yield return this[x, y];
        }
    }
}
